/*
 * Copyright (C) 2010 SHARP Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.sharp.android.decome;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.apache.http.client.AuthenticationHandler;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * SurfaceController/Parallaxを使用したサンプルである。 立体視のSurfaceViewとListViewを重ねて表示する。
 * Menuキーにて立体視有無を制御する。 Backキーにて終了する。
 * AddOnのAPIを使用しているのは、SurfaceViewを拡張したSurfaceView3Dである。
 */
public class SampleStereo3DLCD extends Activity {
	SurfaceView3D mSurfaceView3D = null; /* 立体視有りのSurfaceView */
	SurfaceView3D mSurfaceView2D = null; /* 立体視無しのSurfaceView */
	ListView mListView = null; /* 合成表示されるListView */
	boolean mStereo3D = false; /* 立体視の状態 */
	List<HashMap<String, String>> mVideoHashList = null; /* ListView用のデータ */

	/**
	 * アクティビティ作成時に呼ばれるメソッドである。 全画面設定を行い、SurfaceViewとListViewを重ね合わせ表示する。
	 * 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Reflector.setFilesDir(getFilesDir());
		try {
			copyAssetToLocal("animation.jar", "animation.jar");
		} catch (IOException e) {
			e.printStackTrace();
		}

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.frame);

		make3DSurface();

		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

		mStereo3D = false;
		mSurfaceView3D.setStereoMode(mStereo3D);
	}

	/*
	 * 立体視のSurfaceViewを生成する。
	 */
	private void make3DSurface() {
		FrameLayout fr = (FrameLayout) findViewById(R.id.base_frame);
		mSurfaceView3D = new SurfaceView3D(this);
		LinearLayout.LayoutParams surface_layout = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);
		mSurfaceView3D.setLayoutParams(surface_layout);
		fr.addView(mSurfaceView3D);
		mSurfaceView3D.setStereoMode(mStereo3D);
	}

	/*
	 * 立体視無しのSurfaceViewを生成する。
	 */
	private void make2DSurface() {
		FrameLayout fr = (FrameLayout) findViewById(R.id.base_frame);
		mSurfaceView2D = new SurfaceView3D(this);
		LinearLayout.LayoutParams surface_layout = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);
		mSurfaceView2D.setLayoutParams(surface_layout);
		fr.addView(mSurfaceView2D);
	}

	/*
	 * SurfaceViewの情報(表示サイズ・表示位置）をコピーする。
	 */
	private void copyData(SurfaceView3D src, SurfaceView3D dst) {
		dst.mPosX = src.mPosX;
		dst.mPosY = src.mPosY;
		dst.mVectorX = src.mVectorX;
		dst.mVectorY = src.mVectorY;
		dst.mWidth = src.mWidth;
		dst.mHeight = src.mHeight;
	}

	/*
	 * 立体視の有無を切り替える。
	 */
	private void changeMode() {
		try {
			if (mStereo3D) {
				mStereo3D = false;
				make2DSurface();
				copyData(mSurfaceView3D, mSurfaceView2D);
				mSurfaceView3D.setVisibility(View.INVISIBLE);
				mSurfaceView3D = null;
			} else {
				mStereo3D = true;
				make3DSurface();
				copyData(mSurfaceView2D, mSurfaceView3D);
				mSurfaceView2D.setVisibility(View.INVISIBLE);
				mSurfaceView2D = null;
			}
		} catch (Exception ex) {
		}
		System.gc();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (mSurfaceView3D != null)
			mSurfaceView3D.onPause();
		if (mSurfaceView2D != null)
			mSurfaceView2D.onPause();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (mSurfaceView3D != null)
			mSurfaceView3D.onResume();
		if (mSurfaceView2D != null)
			mSurfaceView2D.onResume();
	}

	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			this.finish();
			break;
		case KeyEvent.KEYCODE_MENU:
			changeMode();
			break;
		default:
			break;
		}

		return (true);
	}

	private void copyAssetToLocal(final String inAssetFilename,
			final String outFilename) throws IOException {
		final BufferedInputStream in = new BufferedInputStream(getAssets()
				.open(inAssetFilename));
		final BufferedOutputStream out = new BufferedOutputStream(
				openFileOutput(outFilename, MODE_PRIVATE));

		int buffer;
		while ((buffer = in.read()) != -1) {
			out.write(buffer);
		}
		out.close();
		in.close();
	}
}
